const baseUrl = getApp().globalData.ip
module.exports = {
  request: (url, method,data) => {
    //这里使用ES6的写法拼接的字符串
    let _url = `${baseUrl}/${url}`;
    let TEXTURL = url.split('/')[0]
    let length = url.split('/').length
    let ENDURL = url.split('/')[length-1]
    return new Promise((resolve, reject) => {
      // header封装
      let contentType = 'application/x-www-form-urlencoded'
      const TOKEN = wx.getStorageSync('TOKEN')
      if (TEXTURL==='opinoin' || ENDURL==='info') {
        contentType = 'application/json'
      }else{
        contentType = 'application/x-www-form-urlencoded'
      }
      wx.request({
        url: _url,
        data: data,
        method: method,
        header: {
          'content-type':contentType,
          'Authorization':TOKEN
        },
        success: (res) => {
          let {code} = res.data;
          if (code === 200) {
            resolve(res.data);
            wx.hideLoading();
          } else {
            wx.showToast({
              title: '数据请求错误',
              icon: 'error',
              duration: 2000
            })
          }
        },
        fail() {
          reject('接口有误，请检查')
        }
      });
    });
  },
}